package com.game.xmldata;

import java.lang.reflect.Field;

import com.game.xmldata.factory.BoxFactory;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SampleManager {

	private static SampleManager sampleManager = new SampleManager();
	public BoxFactory boxFactory;

	public void init(String xmldataConfig) {
		Field[] fields = SampleManager.class.getFields();
		for (Field field : fields) {
			try {
				Class<?> clazz = field.getType();
				AbstractSampleFactory<?> sampleFac = (AbstractSampleFactory<?>) clazz.newInstance();
				field.set(this, sampleFac);
				sampleFac.load(xmldataConfig);
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}

	public static SampleManager get() {
		return sampleManager;
	}
	
	public void reloadXml(String clazzName,String xmldataConfig)
			throws IllegalArgumentException, IllegalAccessException, InstantiationException {
		Field[] fields = SampleManager.class.getFields();
		for (Field field : fields) {
			Class<?> clazz = field.getType();
			String simpleName = clazz.getSimpleName();
			if (simpleName.equals(clazzName)) {
				AbstractSampleFactory<?> sampleFac = (AbstractSampleFactory<?>) clazz.newInstance();
				//field.set(this, sampleFac);
				sampleFac.load(xmldataConfig);
			}
		}
	}
}
